<style src="../../style/contentTable.css" scoped></style>
<template>
  <div class="containerTable">
    <!--表5蔬菜生产经营实体表T_VegProsecution-->
    <Row class="divSearch">
      <Col span="24" class="col-margin">
      <!--根据需求删除不不要查询条件-->
      填报年度：
      <Select v-model="searchModel.FillInYear" placeholder="全部" style="width: 200px" @on-change="GetData()">
        <!-- <Option value="">全部</Option> -->
        <Option :value="item.DictText" v-for="item in CreateYearList" :key="item.DictValue">{{item.DictText}}</Option>
      </Select>
        <Button icon="ios-search" type="ghost" @click="GetData()">查询</Button>               
        <Button icon="ios-download" type="primary" @click="ExcelDown()" style="float:right;margin-right:20px" v-show="DataModel.ReportStatus >1">数据导出</Button>
        <Upload :on-success="handleSuccess" :action="GetPostUrl()" style="float:right;margin-right:20px" v-show="DataModel.ReportStatus <3&&isTime()">
            <Button type="primary" icon="ios-cloud-upload-outline">数据导入</Button>
        </Upload>
        <Button icon="ios-download" type="primary" @click="TmpDown()" style="float:right;margin-right:20px">模板下载</Button>
      </Col>
    </Row>
    <Row>
     <Col class="col-margin" style="padding-top:50px;" v-show="isTime()">
      <Form ref="DataModel" :model="DataModel" :rules="ruleModel" >  
          <table class="reptable1">
              <caption >
                  <label style="float:left;font-size:16px;color:#050505;">{{searchModel.FillInYear}}水果外销统计表</label>
                  <lable style="float:right" v-show="DataModel.ReportStatus==0">[<a @click="ReasonShow=true">未通过原因</a>]</lable>
                  <label style="float:right">当前状态:
                      <span v-if="DataModel.ReportStatus==0" class="Error">退回重报</span> 
                      <span v-else-if="DataModel.ReportStatus==1" class="NoReport">未填写</span>
                      <span v-else-if="DataModel.ReportStatus==2" class="AlreadyReport">已填写</span>
                      <span v-else-if="DataModel.ReportStatus==3" class="UpReport">已上报</span>
                      <span v-else-if="DataModel.ReportStatus==4" class="Success">审核通过</span>               
                      <span v-else class="Error">异常</span>               
                  </label>                  
              </caption>
              <thead>
                  <tr>
                      <th rowspan="2">水果全年生产总量(吨)</th>
                      <th colspan="2">其中外销量（出县）</th>
                       <th rowspan="2">水果全年生产总产值(元)</th>
                      <th colspan="2">其中外销产值（出县）</th>
                  </tr>
                  <tr>
                      <th>水果外销量（出县）（吨）</th>
                      <th>比上年增减%</th>
                      <th>水果外销产值（出县）（元）</th>
                      <th>比上年增减%</th>
                  </tr>
              </thead>
              <tr>            
                  <td>
                      <Poptip trigger="focus" title="上年数据" :content="DataModel.OldProductNumber"> 
                          <Form-item prop="ProductNumber">  
                              <Input v-model="DataModel.ProductNumber"></Input>
                          </Form-item>                          
                          <!-- <div class="api" slot="content">
                              <Row>
                                  <Col span="24">上年数据：{{DataModel.OldProductNumber}}</Col>
                             </Row>
                             <Row>
                                  <Col span="24">比上年增减%：{{DataModel.pecProductNumber}}</Col>
                             </Row>                            
                          </div> -->
                      </Poptip>
                  </td>
                  <td>
                      <Poptip trigger="focus" title="上年数据" :content="DataModel.OldSaleNumber"> 
                          <Form-item prop="SaleNumber">                                
                              <Input v-model="DataModel.SaleNumber"></Input>
                          </Form-item>                         
                          <!-- <div class="api" slot="content">
                              <Row>
                                  <Col span="24">上年数据：{{DataModel.OldSaleNumber}}</Col>
                             </Row>
                             <Row>
                                  <Col span="24">比上年增减%：{{DataModel.pecSaleNumber}}</Col>
                             </Row>                            
                          </div> -->
                      </Poptip>
                  </td>
                    <td  v-if="DataModel.SaleNumber != null&&DataModel.SaleNumber !=''&&DataModel.OldSaleNumber != null&&DataModel.OldSaleNumber != ''&&DataModel.OldSaleNumber*1 !=0">{{DataModel.pecSaleNumber=(DataModel.SaleNumber*1-DataModel.OldSaleNumber*1)/(DataModel.OldSaleNumber*1)*100}}</td>
                    <td v-else>
                        <Form-item prop="pecSaleNumber">
                        <Input v-model="DataModel.pecSaleNumber"></Input>
                        </Form-item>  
                    </td>
                  <td>
                      <Poptip trigger="focus" title="上年数据" :content="DataModel.OldProductMoney"> 
                          <Form-item prop="ProductMoney">                          
                              <Input v-model="DataModel.ProductMoney"></Input>
                          </Form-item>                         
                          <!-- <div class="api" slot="content">
                              <Row>
                                  <Col span="24">上年数据：{{DataModel.OldProductMoney}}</Col>
                             </Row>
                             <Row>
                                  <Col span="24">比上年增减%：{{DataModel.pecProductMoney}}</Col>
                             </Row>                            
                          </div> -->
                      </Poptip>
                  </td>                                                     
                  <td>
                      <Poptip trigger="focus" title="上年数据" :content="DataModel.OldSaleMoney">  
                          <Form-item prop="SaleMoney">
                              <Input v-model="DataModel.SaleMoney"></Input>
                          </Form-item>                          
                          <!-- <div class="api" slot="content">
                              <Row>
                                  <Col span="24">上年数据：{{DataModel.OldSaleMoney}}</Col>
                             </Row>
                             <Row>
                                  <Col span="24">比上年增减%：{{DataModel.pecSaleMoney}}</Col>
                             </Row>                            
                          </div> -->
                      </Poptip>
                  </td> 
                  <td  v-if="DataModel.SaleMoney != null&&DataModel.SaleMoney !=''&&DataModel.OldSaleMoney != null&&DataModel.OldSaleMoney != ''&&DataModel.OldSaleMoney*1 !=0">{{DataModel.pecSaleMoney=(DataModel.SaleMoney*1-DataModel.OldSaleMoney*1)/(DataModel.OldSaleMoney*1)*100}}</td>
                    <td v-else>
                        <Form-item prop="pecSaleMoney">
                        <Input v-model="DataModel.pecSaleMoney"></Input>
                        </Form-item>
                        </td>               
              </tr>
          
          </table> 
      </Form>
          <div  style="text-align:right;padding:10px;">
              <Button type="primary" @click="save('DataModel')" :disabled="DataModel.ReportStatus < 3 && BtnSaveStatus ? false:true">保存</Button>
              <Button type="primary" @click="DataUp(DataModel.ReportId)" :disabled="DataModel.ReportStatus == 2 ? false:true">上报</Button>
          </div>
          <Alert class="alert" banner type="error" style="margin-top:120px;">
              填报时间：每年1月10日</br>
              填报说明: <!-- <span style="color:red">出省的量是外销量，含出口。</span> -->
              1.全省范围内从乡（镇）起，逐级全面统计，由县级填报经州（市）审核后上报。
              <!-- 数据平衡关系:(1)<span style="color:red">生产总量>外销量</span>
              &nbsp;&nbsp;&nbsp;&nbsp;
              (2)<span style="color:red">生产总额>外销额</span>
              <br/>
              特别提醒:(1)<span style="color:red">外销量只在生产总量存在的情况下才存在</span>
              &nbsp;&nbsp;&nbsp;&nbsp;
              (2)<span style="color:red">外销额只在生产总额存在的情况下才存在</span>-->
              <span slot="desc"></span> 
          </Alert>
      </Col>
      <Col class="col-margin" v-show="!isTime()">
         <Alert class="AlterWarm">不在填报时间内，无法填报！</Alert>
      </Col>
    </Row>
     <Modal v-model="ReasonShow" :mask-closable="false" width="500"> 
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">未通过原因</span>
            </p>
            <p slot="close">
                <Icon type="close-circled" color="red" size=25></Icon>
            </p>
            <div class="reasonDiv">{{DataModel.Opinion}}</div>
            <p slot="footer"></p>
        </Modal> 
  </div>
</template>
<script>
//import { getList } from 'api/Report';
//默认http请求配置
import axios from '../../config'
export default {
  data() {
     const validateData = (rule, value, callback) => {
            var temp = /^\-?[0-9]\d*(\.\d+)?$/;
            if (value != null && value != "") {
                if (temp.test(value) == false) {
                    callback(new Error('必须是数字！'));
                }
                else {
                    callback();
                }
            } else {
                callback();
            }
        };
    return {
      ReasonShow:false,//是否显示未通过原因Modal      
      ViewShow: false,//查看状态
      TableCodeList: this.GetGroupDict("ReportTableName"),//表名称
      CreateYearList: this.GetGroupDict("ReportYear"),//周期
      CycleList: this.GetGroupDict("ReportCycle"),//
      CurrentYear:'',//当前年度,
      BtnSaveStatus:false,
      DataModel:{//数据表代码
          Id:'',
          ReportId:'',
          ProductNumber:'',//生产总量
          SaleNumber:'',//销售量
          ProductMoney:'',//
          SaleMoney:'',
          ReportStatus:'', 
          Opinion:'' ,//审核未通过原因                  
          //********去年的数据********
          OldId:'',
          OldReportId:'',
          OldProductNumber:'',//生产总量
          OldSaleNumber:'',//销售量
          OldProductMoney:'',//
          OldSaleMoney:'',
          OldReportStatus:'' , 
          //********环比********  
          pecProductNumber:'',
          pecSaleNumber:'',
          pecProductMoney:'' ,
          pecSaleMoney:''     
      },
      ruleModel:{
          ProductNumber: [{ validator: validateData, trigger: 'blur' }],
          SaleNumber:[{ validator: validateData, trigger: 'blur' }],//销售量
          pecSaleNumber:[{ validator: validateData, trigger: 'blur' }],
          ProductMoney:[{ validator: validateData, trigger: 'blur' }],//
          SaleMoney:[{ validator: validateData, trigger: 'blur' }],
          pecSaleMoney:[{ validator: validateData, trigger: 'blur' }]
      },
      //查询模型：根据需求删除不不要查询条件
      searchModel: {
        ReportCode: 'FruExport',
        FillInYear: '',
        TimesType: '',
        AreaCode: this.GetlocaStorage("fruuserinfo").AreaCode,
      },
    }
  },
  methods: {
       //获取今往年数据
        GetData() {
            var self = this;
            var model = new Object();
            model.ReportCode = self.searchModel.ReportCode;
            if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
                model.FillInYear = self.searchModel.FillInYear;
            } else {
                model.FillInYear = self.CurrentYear + '年';//填报年度-----数据字典值
                self.searchModel.FillInYear = self.CurrentYear + '年';
            }
            model.AreaCode = self.searchModel.AreaCode;
            self.DataModel={};//清空当前数据            
            self.$http.post("Report/PostData", model).then( //获取数据编辑项的值
                function (res) {
                    if (res.data.list.status == 2 || res.data.list.status == 1) {
                        self.DataModel = res.data.list.model;
                        // if(self.DataModel.OldProductNumber&&self.DataModel.ProductNumber){
                        //     self.DataModel.pecProductNumber = (self.DataModel.ProductNumber*1-self.DataModel.OldProductNumber*1)/(self.DataModel.OldProductNumber*1)
                        // }
                        // if(self.DataModel.OldSaleNumber&&self.DataModel.SaleNumber){
                        //     self.DataModel.pecSaleNumber = (self.DataModel.SaleNumber*1-self.DataModel.OldSaleNumber*1)/(self.DataModel.OldSaleNumber*1)
                        // }
                        // if(self.DataModel.OldProductMoney&&self.DataModel.ProductMoney){
                        //     self.DataModel.pecProductMoney = (self.DataModel.ProductMoney*1-self.DataModel.OldProductMoney*1)/(self.DataModel.OldProductMoney*1)
                        // }
                        // if(self.DataModel.OldSaleMoney&&self.DataModel.SaleMoney){
                        //     self.DataModel.pecSaleMoney = (self.DataModel.SaleMoney*1-self.DataModel.OldSaleMoney*1)/(self.DataModel.OldSaleMoney*1)
                        // }
                        self.BtnSaveStatus = true;
                    } else {
                        self.DataModel = res.data.list.model;
                        self.$Modal.warning({
                            title: "提示信息",
                            content: res.data.list.message
                        });
                        self.BtnSaveStatus = false;
                    }
                }
            );
        },
        //模板下载
        TmpDown:function(){
        window.open(this.$http.defaults.baseURL + "Report/GetTemplateData?ReportCode=" + this.searchModel.ReportCode);
        },
        //上报
        DataUp(id) {
            var self = this;
            self.$Modal.confirm({
                title: '提示信息',
                content: '<p>是否确认上报？</p>',
                onOk: () => {
                var model = new Object();
                model.Id = id;
                model.reportType = '3';
                model.strMsg = '';
                this.$http.post("Report/PostUpStatus", model).then(
                    function (res) {
                        if (res.data.status == 1) {
                            self.$Notice.success({ title: '已上报！', desc: false });
                            self.GetData();
                        }
                        else {
                            self.$Modal.error({ title: "提示信息", content: "上报失败!!!" });
                        }
                    });
                }
            });
        },
        //保存方法
        save() {
            var self = this;
            if(this.isTime()){
                this.$refs["DataModel"].validate((valid) =>{
                    if(valid){
                        if (self.DataModel.SaleNumber*1 > self.DataModel.ProductNumber*1) {
                            self.$Modal.warning({ title: "提示信息", content: "外销量不应大于水果全年生产总量!" });
                        } 
                        else if (self.DataModel.SaleMoney*1 > self.DataModel.ProductMoney*1) {
                            self.$Modal.warning({ title: "提示信息", content: "外销额不应大于水果全年生产总额!" });
                        }
                        else {
                            this.$http.put('FruExport', self.DataModel).then(function (res) {
                                if (res.data.status == 1) {
                                    self.GetData();//读取分页数据
                                    //self.modalHide();//关闭modal
                                    self.$Notice.success({ title: '保存成功！', desc: false });
                                    }
                                else {
                                    self.$Modal.warning({ title: "提示信息", content: "更新失败,请与管理员联系!" });
                                }
                            });
                        }
                 }
                 });
            }
            else{
                this.$Modal.warning({ title: "提示信息", content: "不在填报时间，无法填报!" });
             }
            },
             //导入
            GetPostUrl() {
                var url = this.$http.defaults.baseURL + "FruExport/PostImportData?ReportCode=" + this.searchModel.ReportCode + "&&AreaCode=" + this.searchModel.AreaCode+"&&FillInYear=" + this.searchModel.FillInYear;
                return url;
            },
             //导出
            ExcelDown: function () {
                var self = this;
                var model = new Object();
                model.ReportCode = self.searchModel.ReportCode;
                if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
                    model.FillInYear = self.searchModel.FillInYear;
                } else {
                    model.FillInYear = self.CurrentYear + '年';//填报年度-----数据字典值
                    self.searchModel.FillInYear = self.CurrentYear + '年';
                }
                model.AreaCode = self.searchModel.AreaCode;
                window.open(this.$http.defaults.baseURL + "FruExport/GetExcelData?ReportCode=" + model.ReportCode + "&&FillInYear=" + model.FillInYear + "&&AreaCode=" + model.AreaCode);
            },
            handleSuccess(res, file) {
                var self = this;
                self.GetData();
                self.$Notice.success({
                    title: '数据导入成功！',
                    desc: false
                });
            },
            showReason(){
                this.Reason = this.DataModel.Opinion;
                this.ReasonShow = true;
            },
            currentYear(){
                let year = new Date().getFullYear();
                let month = new Date().getMonth();                
                if(month>1){
                    this.CurrentYear = year;
                }
                else{
                    this.CurrentYear = year - 1;
                }                
            },
            isTime(){
                let month = new Date().getMonth(); 
                let year = new Date().getFullYear()+"年"; 
                if(month>2 && month<10&&(this.searchModel.FillInYear==year.toString())){
                    return false;
                }
                else{
                    return true;
                }
            }

  },
  //创建Vue页面方法
  created: function () {
    //创业Vue前调用获取this.datas: {},//table数据对象
    this.currentYear();
    this.GetData();   
  },
  watch: {
    //需要监听的在此处新增监听对象
  }
}
</script>
